var DtoConverter = {
    toYearAndTravelDto: function(yearAndTravelJsonDto) {
        var yearJsonDto = yearAndTravelJsonDto.year;
        var travelJsonDtos = yearAndTravelJsonDto.travel;
        var yearDto = yearJsonDto.select(function(monthJsonDto) {
            return DtoConverter.toMonthDto(monthJsonDto);
        });
        var travelDtos = travelJsonDtos.select(function(travelJsonDto) {
            return DtoConverter.toTravelStepsDto(travelJsonDto);
        });
        return { year: yearDto, travel: travelDtos };
    },
    toTravelStepsJsonDto: function(travelStepDto) {
        return {
            DL: travelStepDto.departureLocation.id,
            AL: travelStepDto.arrivalLocation.id,
            DD: travelStepDto.departureDateTime,
            AD: travelStepDto.arrivalDateTime,
            DA: travelStepDto.departureActivity,
            AA: travelStepDto.arrivalActivity,
            Id: travelStepDto.tid
        };
    },
    toTravelStepsDto: function(travelStepJsonDto) {
        return {
            departureLocation: pwc.locationCache.getByLid(travelStepJsonDto.DL),
            arrivalLocation: pwc.locationCache.getByLid(travelStepJsonDto.AL),
            departureDateTime: travelStepJsonDto.DD,
            arrivalDateTime: travelStepJsonDto.AD,
            departureActivity: travelStepJsonDto.DA,
            arrivalActivity: travelStepJsonDto.AA,
            tid: travelStepJsonDto.Id
        };
    },

    toMonthDto: function(monthJsonDto) {
        return monthJsonDto ?
        {
            "month": monthJsonDto.m,
            "year": monthJsonDto.y,
            "days": monthJsonDto.d.select(function(dayJsonDto) {
                return DtoConverter.toDayDto(dayJsonDto, monthJsonDto.y, monthJsonDto.m);
            }),
            "completeStatus": monthJsonDto.cs
        }
        : null;
    },

    toMonthJsonDto: function(monthDto) {
        var dto = {
            "m": monthDto.month,
            "y": monthDto.year,
            "cs": monthDto.completeStatus
        };
        if (monthDto.days !== undefined) {
            dto["d"] = monthDto.days.select(function(day) {
                return DtoConverter.toDayJsonDto(day);
            });
        }
        return dto;
    },

    toDayJsonDto: function(dayDto) {
        var json = {};
        json["d"] = dayDto.date.substring(dayDto.date.length - 2);
        if (dayDto.location) {
            json["l"] = dayDto.location.id;
        }
        if (dayDto.activity != "") {
            json["a"] = dayDto.activity;
        }
        if (dayDto.secondActivity != "") {
            json["sa"] = dayDto.secondActivity;
        }
        return json;
    },

    toDayDto: function(dayJsonDto, year, month) {
        return {
            "date": Date.parse(year + "-" + month + "-" + dayJsonDto.d).toString(CalendarConstants.JSONDateFormat),
            "location": dayJsonDto.l ? pwc.locationCache.getByLid(dayJsonDto.l) : null,
            "activity": dayJsonDto.a || "",
            "secondActivity": dayJsonDto.sa || ""
        };
    }
};